package com.controller;

import com.dao.StudentDao;
import com.model.Student;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/student/*")
public class StudentServlet extends HttpServlet {
    private StudentDao studentDao = new StudentDao();
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        
        try {
            if ("/list".equals(pathInfo)) {
                List<Student> students = studentDao.getAllStudentList();
                req.setAttribute("students", students);
                req.getRequestDispatcher("/WEB-INF/views/student/list.jsp").forward(req, resp);
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        
        try {
            if ("/add".equals(pathInfo)) {
                addStudent(req, resp);
            } else if ("/update".equals(pathInfo)) {
                updateStudent(req, resp);
            } else if ("/delete".equals(pathInfo)) {
                deleteStudent(req, resp);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
    
    private void addStudent(HttpServletRequest req, HttpServletResponse resp) 
            throws SQLException, ParseException, IOException {
        Student student = getStudentFromRequest(req);
        studentDao.addStudentEntity(student);
        resp.sendRedirect(req.getContextPath() + "/student/list");
    }
    
    private void updateStudent(HttpServletRequest req, HttpServletResponse resp) 
            throws SQLException, ParseException, IOException {
        Student student = getStudentFromRequest(req);
        studentDao.updateStudent(student);
        resp.sendRedirect(req.getContextPath() + "/student/list");
    }
    
    private void deleteStudent(HttpServletRequest req, HttpServletResponse resp) 
            throws SQLException, IOException {
        int sno = Integer.parseInt(req.getParameter("sno"));
        studentDao.deleteStudent(sno);
        resp.sendRedirect(req.getContextPath() + "/student/list");
    }
    
    private Student getStudentFromRequest(HttpServletRequest req) throws ParseException {
        Student student = new Student();
        student.setSno(Integer.parseInt(req.getParameter("sno")));
        student.setSname(req.getParameter("sname"));
        student.setSsex(Integer.parseInt(req.getParameter("ssex")));
        student.setSage(Integer.parseInt(req.getParameter("sage")));
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        student.setBirthday(sdf.parse(req.getParameter("birthday")));
        
        student.setSdept(req.getParameter("sdept"));
        return student;
    }
} 